﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;

namespace StudyEnglish
{
    public partial class UserLogin : PhoneApplicationPage
    {
        public UserLogin()
        {
            InitializeComponent();
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void LoginProcess()
        {
            App.UserVM.getUser(txt_username.Text, txt_password.Password);
            if (App.UserVM.LstUser.Count() == 0)
            {
                MessageBox.Show("Tên đăng nhập hoặc mật khẩu không chính xác", "Thông Báo", MessageBoxButton.OK);
                txt_username.Focus();
            }
            else
            {
                App.userID = App.UserVM.LstUser[0].ID;
                App.Volnum = App.UserVM.LstUser[0].CountWord;
                NavigationService.Navigate(new Uri("/DailyList.xaml", UriKind.Relative));
            }
        }

        private void btn_login_Click(object sender, RoutedEventArgs e)
        {
            LoginProcess();
        }

        private void txt_password_KeyUp(object sender, KeyEventArgs e)
        {
            var isEnterKey = e.Key == System.Windows.Input.Key.Enter || e.PlatformKeyCode == 10;

            if (isEnterKey)
            {
                LoginProcess();
            }
        }
    }
}